package com.sxz.sbs.module.staff;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.constant.IntentConstant;
import com.sxz.sbs.constant.TypeConstant;
import com.sxz.sbs.event.OperateSuccessEvent;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.staff.adapter.StaffDisposeAdapter;
import com.sxz.sbs.module.staff.mode.StaffDisposeBean;
import com.sxz.sbs.module.staff.mode.StaffIntentBean;
import com.sxz.sbs.module.staff.presenter.StaffDisposePresenter;
import com.sxz.sbs.module.staff.view.StaffDisposeView;
import com.sxz.sbs.widget.CommonTitle;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 员工处理选择列表  --- 交换对接 或 离职
 *
 * @author hp
 */
public class StaffDisposeActivity extends BaseActivity<StaffDisposePresenter> implements StaffDisposeView {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private List<StaffDisposeBean.ListBean> dataList = new ArrayList<>();
    private StaffDisposeAdapter disposeAdapter;
    private StaffIntentBean staffBean;
    private int currentPage = 1;

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        staffBean = getIntent().getParcelableExtra(IntentConstant.STAFF_INTENT_BEAN);
        if (staffBean != null) {
            new CommonTitle.Builder(this).setTitle(staffBean.getParkingName() + "员工").build().show();
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            refreshLayout.setColorSchemeResources(R.color.color_main);
            refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    currentPage = 1;
                    getStaff();
                }
            });
            refreshLayout.setRefreshing(true);
            getStaff();
            if (staffBean.getDisposeType() == TypeConstant.STAFF_MANGER_SWAP || staffBean.getDisposeType() == TypeConstant.STAFF_MANGER_SWAP_OUT) {
                disposeAdapter = new StaffDisposeAdapter(StaffDisposeAdapter.DisposeEnum.SWAP, dataList);
            } else {
                disposeAdapter = new StaffDisposeAdapter(StaffDisposeAdapter.DisposeEnum.DIMISSION, dataList);
            }
            recyclerView.setAdapter(disposeAdapter);
            recyclerView.addOnItemTouchListener(new OnItemClickListener() {
                @Override
                public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                    if (staffBean.getDisposeType() == TypeConstant.STAFF_MANGER_SWAP) {
                        //ToastUtils.showShort(dataList.get(position).getTxt_Name());
                        staffBean.setDisposeId(dataList.get(position).getId());
                        startActivity(new Intent(StaffDisposeActivity.this, ChoseStaffActivity.class)
                                .putExtra("staff_bean", staffBean)
                                .putExtra("chose_tag", ChoseStaffActivity.CHOSE_STAFF_SWAP_IN)
                        );

                    } else if (staffBean.getDisposeType() == TypeConstant.STAFF_MANGER_SWAP_OUT) {
                        staffBean.setDisposeId(dataList.get(position).getId());
                        startActivity(new Intent(StaffDisposeActivity.this, AllParkingActivity.class)
                                .putExtra("staff_bean", staffBean)
                        );
                    } else {
                        staffBean.setDisposeId(dataList.get(position).getId());
                        staffBean.setHeadUrl(dataList.get(position).getUrl_HeadPhoto());
                        staffBean.setStaffName(dataList.get(position).getTxt_Name());
                        staffBean.setStaffPhone(dataList.get(position).getTxt_Phone());
                        startActivity(new Intent(StaffDisposeActivity.this, StaffDimissionActivity.class)
                                .putExtra("staff_dimission", staffBean));
                    }
                }
            });
            disposeAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
                    currentPage++;
                    getStaff();
                }
            }, recyclerView);

            stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
                @Override
                public void onRetry(int state) {
                    getStaff();
                }
            });


        } else {
            new CommonTitle.Builder(this).setTitle("加载出错...").build().show();
        }

    }

    /**
     * 获取员工列表
     */
    private void getStaff() {
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("parkingId", staffBean.getParkingId());
        map.put("pageNumber", currentPage);
        map.put("pageSize", AppConstant.PAGE_SIZE);
        mPresenter.getStaffByPId(map);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_staff_dispose;
    }

    @Override
    protected StaffDisposePresenter createPresenter() {
        return new StaffDisposePresenter();
    }

    @Override
    public void onGetStaffSuccess(StaffDisposeBean bean) {
        if (mIsLive && bean != null) {
            refreshLayout.setRefreshing(false);
            if (bean.getList() != null && bean.getList().size() > 0) {
                stateLayout.showContent();
                if (currentPage == 1) {
                    dataList.clear();
                    dataList.addAll(bean.getList());
                    disposeAdapter.notifyDataSetChanged();
                } else {
                    dataList.addAll(bean.getList());
                    disposeAdapter.notifyDataSetChanged();
                }
                disposeAdapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    dataList.clear();
                    disposeAdapter.notifyDataSetChanged();
                    //  数据为空
                    stateLayout.showEmpty();
                } else {
                    if (dataList.size() > AppConstant.PAGE_SIZE) {
                        disposeAdapter.loadMoreEnd();
                    } else {
                        disposeAdapter.loadMoreEnd(true);
                    }
                }
            }

        }
    }

    /**
     * 此界面无用
     */
    @Override
    public void onSwapSuccess() {
    }

    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
        if (currentPage == 1) {
            if (errorTag == AppConstant.NET_ERROR_CODE) {
                stateLayout.showNetwork();
            } else {
                stateLayout.showEmpty();
            }
        } else {
            disposeAdapter.loadMoreFail();
        }
    }

    @Override
    public void loginAgain() {
        super.loginAgain();
        refreshLayout.setRefreshing(false);
        disposeAdapter.loadMoreFail();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOperateSuccessEvent(OperateSuccessEvent event) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
